package com.csweimei.rentmanagementtreasure.tenant.ui.act.customer;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProviders;

import android.os.Bundle;
import android.widget.RadioGroup;

import com.chad.library.BR;
import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.adapter.CustomerTypeAdapter;
import com.csweimei.rentmanagementtreasure.base.AppViewModelFactory;
import com.csweimei.rentmanagementtreasure.databinding.ActivityAddCustomerBinding;
import com.csweimei.rentmanagementtreasure.pop.PopBean;
import com.csweimei.rentmanagementtreasure.tenant.viewModel.AddCustomerVM;
import com.csweimei.rentmanagementtreasure.utils.ValidateUtils;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.base.BaseActivity;

public class AddCustomerActivity extends BaseActivity<ActivityAddCustomerBinding, AddCustomerVM> {


    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_add_customer;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }


    String customerId;
    String flag;
    @Override
    public void initParam() {
        super.initParam();
        flag = getIntent().getStringExtra("flag");
        customerId = getIntent().getStringExtra("id");

    }

    @Override
    public void initData() {
        super.initData();
        viewModel.setActivity(this);

        if (flag.equals("add")){
            viewModel.setTitleText("添加客户");
        }else {
            viewModel.setTitleText("编辑客户");
        }

        if (ValidateUtils.isNotEmptyString(customerId)){
            viewModel.getCustomerData(customerId);
        }


        if (binding.cbMan.isChecked()){
            binding.cbWman.setChecked(false);
            viewModel.customerBean.get().setSex("男");
        }

        if (binding.cbWman.isChecked()){
            binding.cbMan.setChecked(false);
            viewModel.customerBean.get().setSex("女");
        }

        binding.rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId  == R.id.cb_man){
                    viewModel.customerBean.get().setSex("男");
                }else {
                    viewModel.customerBean.get().setSex("女");
                }
            }
        });

        CustomerTypeAdapter customerTypeAdapter = new CustomerTypeAdapter();
        customerTypeAdapter.addChildClickViewIds(R.id.item);
        customerTypeAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            customerTypeAdapter.setSel(position);
            viewModel.type.set(customerTypeAdapter.getData().get(position).getName());
            viewModel.customerBean.get().customType =  viewModel.type.get();
        });
        List<PopBean> list = new ArrayList<>();
        list.add(new PopBean("A级客户","重点关注"));
        list.add(new PopBean("B级客户","日常维护"));
        list.add(new PopBean("C级客户","仅做记录"));
        customerTypeAdapter.setList(list);

        binding.typeRecy.setAdapter(customerTypeAdapter);
    }

    @Override
    public AddCustomerVM initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this,factory).get(AddCustomerVM.class);
    }
}